package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.CmsContentBiz;
import cn.sumpu.app.guoguojie.biz.CommentBiz;
import cn.sumpu.app.guoguojie.biz.DiscussBiz;
import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.PictorialTopicBiz;
import cn.sumpu.app.guoguojie.dao.DiscussDao;
import cn.sumpu.app.guoguojie.domain.CmsContent;
import cn.sumpu.app.guoguojie.domain.Comment;
import cn.sumpu.app.guoguojie.domain.Discuss;
import cn.sumpu.app.guoguojie.domain.DiscussTag;
import cn.sumpu.app.guoguojie.domain.DiscussTagDis;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.DoingPictorial;
import cn.sumpu.app.guoguojie.domain.DoingPicture;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.PictorialDoing;
import cn.sumpu.app.guoguojie.domain.PictorialTopic;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.Constant;
import cn.sumpu.app.guoguojie.util.DateUtil;
import cn.sumpu.app.guoguojie.util.PageModel;

public class DiscussBizImpl implements DiscussBiz {
    @Resource
    private IdentifierBiz identifierBiz;
    
    @Resource
    private CmsContentBiz cmsContentBiz;
    @Autowired
    private CommentBiz commentBiz;
    
    @Autowired
    private DoingBiz doingBiz;
    @Autowired
    private PictorialTopicBiz pictorialTopicBiz;
    
    private DiscussDao    discussDao;

    public void setDiscussDao(DiscussDao discussDao) {
        this.discussDao = discussDao;
    }

    /**
     * 新增帖子
     * @param discuss
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#insertDiscuss(cn.sumpu.app.guoguojie.domain.Discuss)
     */
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public void insertDiscuss(Discuss discuss,String picTitle) throws AppBusinessException {
        //创建主题
        PictorialTopic pictorialTopic =new PictorialTopic();
        pictorialTopic.setIdentifier(identifierBiz.generateId("pictorial_topic"));
        pictorialTopic.setName(discuss.getTitle());
        pictorialTopic.setType(Constant.PIC_TOPIC_TYPE_DISCUSS);
        pictorialTopicBiz.addPictorialTopic(pictorialTopic);
        //创建画报
        if(discuss.getTheme()!=null){
            if(discuss.getTheme().equals("02")){
                DoingPictorial dp =new DoingPictorial();
                if(picTitle.equals("")){
                    dp.setTitle(discuss.getTitle());
                }else{
                    dp.setTitle(picTitle);
                }
                pictorialTopicBiz.saveTopicPictorial(pictorialTopic.getIdentifier(), dp);
            }
        }
        discuss.setIdentifier(identifierBiz.generateId("discuss"));
        discuss.setThemeId(pictorialTopic.getIdentifier());
        discussDao.insertDiscuss(discuss);
    }

    /**
     * 更新帖子
     * @param discuss
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#updateDiscuss(cn.sumpu.app.guoguojie.domain.Discuss)
     */
    @Override
    public void updateDiscuss(Discuss discuss) throws AppBusinessException {
        discussDao.updateDiscuss(discuss);

    }
    /**
     * 根据条件查询所有帖子
     * @param page
     * @param pagesize
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#getDiscussList(java.lang.Integer, java.lang.Integer)
     */
    @Override
    public PageModel getDiscussList(Integer page, Integer pagesize) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startId", (page - 1) * pagesize);
        map.put("pageSize", pagesize);
        int count = discussDao.getDiscussCount(map); 
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        List<Discuss> discussList = discussDao.getDiscussList(map);
        pm.setList(discussList);
        pm.setPageSize(pagesize);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + pagesize - 1) / pagesize);
        return pm;
    }
    /**
     * 具体文稿内容
     * @param id(cmsContent标识)
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#getDiscuss(java.lang.String)
     */
    @Override 
    public Discuss getDiscuss(String id) throws AppBusinessException { 
        //推送库信息
        CmsContent cmsContent =cmsContentBiz.getContentById(id);
        if(cmsContent!=null){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("identifier", cmsContent.getTblIdentifier());
            Discuss discuss =discussDao.getDiscuss(map); 
            return discuss;
        }
        return null;
    }
    /**
     * 文稿的评论
     * @param page,id(cmsContent标识)
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#getCommentList(java.lang.Integer)
     */
    
    public PageModel getCommentList(String id,Integer page) throws AppBusinessException { 
        CmsContent cmsContent =cmsContentBiz.getContentById(id);
        if(cmsContent!=null){
        // 
              String type="discuss";
            PageModel pm=commentBiz.getCommentListByDiscuss(cmsContent.getTblIdentifier(),type,page);
            return pm;
        }
        return null;
    }
    /**
     * 增加评论
     * @param comment
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#insertComment(cn.sumpu.app.guoguojie.domain.Comment)
     */
    @Override
    public void insertComment(Comment comment)throws AppBusinessException {
        comment.setIdentifier(identifierBiz.generateId("comment"));
        commentBiz.insert(comment);
    }
    /**
     * 具体文稿内容
     * @param id(discuss标识)
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#getDiscussByDid(java.lang.String)
     */
    @Override 
    public Discuss getDiscussByDid(String id) throws AppBusinessException { 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", id);
        Discuss discuss =discussDao.getDiscuss(map); 
        return discuss;
    }
    /**
     * 增加文稿标签
     * @param discussTag
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#insertDiscussTag(cn.sumpu.app.guoguojie.domain.DiscussTag)
     */
    @Override
    public void insertDiscussTag(DiscussTag discussTag) throws AppBusinessException {
        discussTag.setIdentifier(identifierBiz.generateId("discuss_tag"));
        discussDao.insertDiscussTag(discussTag);

    }
    /**
     * 删除
     */
    @Override
    public void deleteDiscussTagById(String tagId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", tagId);
        discussDao.deleteDiscussTagById(map);

    }
    /**
     * 删除话题
     */
    @Override
    public void deleteDis(String id) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", id);
        discussDao.deleteDis(map);
    }
    /**
     * 
     */
    @Override
    public void deleteDTSById(String id) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("discuss", id);
        discussDao.deleteDTSById(map);
    }
    
    /**
     * 给文稿打标签(关联表)
     * @param discussTag
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#insertDiscussTag(cn.sumpu.app.guoguojie.domain.DiscussTag)
     */
    @Override
    public void insertDiscussTagDis(DiscussTagDis discussTagDis) throws AppBusinessException {
        discussDao.insertDiscussTagDis(discussTagDis); 

    }
    
    /**
     * 根据频道Id查询标签集合
     * @param cid
     * @return
     * @throws AppBusinessException
     */
    public List<DiscussTag> getTagByCid(String cid)throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("channel", cid);
        return discussDao.getTagByCid(map);
    }
    /**
     * 根据频道Id查询发布稿子详情
     */
    @Override 
    public CmsContent getinDiscussContentById(String id) throws AppBusinessException { 
        CmsContent cmsContent =cmsContentBiz.getContentById(id);
        Discuss discuss=  getDiscuss(id);
        if(discuss!=null){
        //主题
        cmsContent.setTheme(discuss.getTheme());
        //回复数
        cmsContent.setReplyAmout(discuss.getReplyAmout());
        //访问数
        cmsContent.setVisitAmout(discuss.getVisitAmout());
        //是否回复
        cmsContent.setIsReply(discuss.getIsReply());
        //是否回复图片
        cmsContent.setIsReplyPic(discuss.getIsReplyPic());
        //创建人
        cmsContent.setNickname(discuss.getCreator());
        //原始内容
        cmsContent.setDescription(discuss.getContent());
        //关联的画报
        cmsContent.setPictorialList(pictorialTopicBiz.getPictorialListByTid(discuss.getThemeId()));
        }
        return cmsContent;
    }
    /**
     * 更新浏览数
     * @param discuss
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#updateDiscuss(cn.sumpu.app.guoguojie.domain.Discuss)
     */
    @Override
    public void updateDiscussAoumt(String id) throws AppBusinessException {
        
        Discuss discuss=  getDiscuss(id);
        if(discuss!=null){
            discuss.setVisitAmout(discuss.getVisitAmout()+1);
            discussDao.updateDiscuss(discuss);
        }
    }
    /**
     * 根据频道Id查询发布稿子列表
     */
    @Override 
    public List<CmsContent> getContentListById(String amoyId,Integer num) throws AppBusinessException {
        List<CmsContent> cmsContentList =cmsContentBiz.getContentList(amoyId, num);
         
        for(CmsContent cms:cmsContentList ){ 
            Discuss discuss=  getDiscuss(cms.getIdentifier());
            if(discuss!=null){
                cms.setPictorialList(pictorialTopicBiz.getPictorialListByTid(discuss.getThemeId()));
                cms.setVisitAmout(discuss.getVisitAmout());
                cms.setTheme(discuss.getTheme());
            }
        }
       /* Discuss discuss=  getDiscuss(id);
        //主题
        cmsContent.setTheme(discuss.getTheme());
        //回复数
        cmsContent.setReplyAmout(discuss.getReplyAmout());
        //访问数
        cmsContent.setVisitAmout(discuss.getVisitAmout());
        //是否回复
        cmsContent.setIsReply(discuss.getIsReply());
        //是否回复图片
        cmsContent.setIsReplyPic(discuss.getIsReplyPic());
        //创建人
        cmsContent.setNickname(discuss.getCreator());
        //关联的画报
        cmsContent.setPictorialList(pictorialTopicBiz.getPictorialListByTid(discuss.getThemeId()));*/
        return cmsContentList;
    }
    /**
     * 
     * @param amoyId
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.DiscussBiz#getAmoysList(java.lang.String)
     */
    @Override 
    public List<CmsContent> getAmoysList(Integer pageSize) throws AppBusinessException {
        List<CmsContent> cmsContentList =cmsContentBiz.getAmoysList(pageSize);
        for(CmsContent cms:cmsContentList ){ 
            Discuss discuss=  getDiscuss(cms.getIdentifier());
            if(discuss!=null){
                cms.setPictorialList(pictorialTopicBiz.getPictorialListByTid(discuss.getThemeId()));
                cms.setVisitAmout(discuss.getVisitAmout());
                cms.setTheme(discuss.getTheme());
            }
        }
        return cmsContentList;
    }
    //话题列表页
    @Override
    public PageModel getCmsContentListForAmoy(String cel,Integer page) throws AppBusinessException {
        if (Common.empty(page)) {
            page = 1;
        }
        int pagesize =20;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startId", (page - 1) * pagesize);
        map.put("pageSize", pagesize);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        //列表
        if((cel).equals("77aa590e-c6e4-4183-9c09-e3a210480251")){
            //0-1岁频道 
            map.put("channel", "5e02f483-87bd-4d77-81d5-a64ecdc877d0");
            //1-3岁频道
            map.put("channel1", "1d48a3b9-3709-4270-81b5-0267a5b3546f");
            //3-6岁频道
            map.put("channel2", "b22c5b02-baa2-42de-ac92-09042e8ccfa2");
            //怀孕期频道
            map.put("channel3", "c1fd788e-590b-4579-b5ef-d21a9f42c92d");
          
        }else{
            map.put("channel", cel);
        }
        List<CmsContent> cmsContentList =cmsContentBiz.getCmsContentListForAmoy(map);
        for(CmsContent cms:cmsContentList ){ 
            Discuss discuss=  getDiscuss(cms.getIdentifier());
            if(discuss!=null){
                cms.setPictorialList(pictorialTopicBiz.getPictorialListByTid(discuss.getThemeId()));
                cms.setVisitAmout(discuss.getVisitAmout());
                cms.setTheme(discuss.getTheme());
                }
        }
        //数目
        int count = cmsContentBiz.getCmsContentListForAmoyCount(map); 
        pm.setList(cmsContentList); 
        pm.setPageSize(pagesize);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + pagesize - 1) / pagesize);
        return pm;
    }
    //标签查话题
    @Override
    public PageModel getdislistt(String cel,String tagId,Integer page) throws AppBusinessException {
        if (Common.empty(page)) {
            page = 1;
        }
        int pagesize =20;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startId", (page - 1) * pagesize);
        map.put("pageSize", pagesize);
        //频道
        map.put("channel", cel);
        //标签
        map.put("tagId", tagId);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        List<CmsContent> cmsContentList =cmsContentBiz.getCmsContentListForAmoy(map);
        for(CmsContent cms:cmsContentList ){ 
            Discuss discuss=  getDiscuss(cms.getIdentifier());
            if(discuss!=null){
                cms.setPictorialList(pictorialTopicBiz.getPictorialListByTid(discuss.getThemeId()));
                cms.setVisitAmout(discuss.getVisitAmout());
                cms.setTheme(discuss.getTheme());
            }
        }
        //数目
        int count = cmsContentBiz.getCmsContentListForAmoyCount(map); 
        pm.setList(cmsContentList); 
        pm.setPageSize(pagesize);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + pagesize - 1) / pagesize);
        return pm;
    }
    //发表图片微波
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    @Override
    public void insertDoingPic(String picUrl,String content,String huabaoId,String ip,Member member,Integer height200xxx,Integer width200xxx) throws Exception {
        //微博
       Doing doing =new Doing();
       doing.setIdentifier(identifierBiz.generateId("doing"));
       doing.setMember(member.getIdentifier());
       doing.setContent(content);
       //doingBiz
       doingBiz.postDoingProduct(doing, null, ip, false, member);
       //图片
       DoingPicture tupianWeibo =new DoingPicture();
       tupianWeibo.setDoing(doing.getIdentifier());
       tupianWeibo.setMember(member.getIdentifier());
       tupianWeibo.setPicUrl(picUrl);
       tupianWeibo.setHeight200xxx(height200xxx);
       tupianWeibo.setWidth200xxx(width200xxx);
       doingBiz.insertDoingPicture(tupianWeibo);
       //微博画报关联表 
       PictorialDoing pd  =new PictorialDoing();
       pd.setDoing(doing.getIdentifier());
       pd.setPictorial(huabaoId);
       pictorialTopicBiz.savePictorialDoing(pd);
    }
    //查询已有标签
    @Override 
    public List<DiscussTagDis> getDTSList(String dId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("discuss", dId);
        List<DiscussTagDis> dtsList =discussDao.getDTSList(map);
        return dtsList;
    }
    //查询话题
    @Override
    public List<Discuss> getDiscussBruce(String time) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("time", DateUtil.parseDate(time));
        return discussDao.getDiscussBruce(map);
    }
    //修改量值
    @Override
    public void updateDiscussAdd(Discuss discuss) throws AppBusinessException {
        discussDao.updateDiscussAdd(discuss);
    }
    //查看画报的评论
    @Override
    public PageModel getPLCommentList(String plid,Integer page) throws AppBusinessException { 
        // 
        if(!("").equals(plid)){
            String type="pictorial";
            PageModel pm=commentBiz.getCommentListByDiscuss(plid,type,page);
            return pm;
        }
        return null;
    }
    //
    @Override
    public DoingPictorial getOneForcreator(String member,String plid)throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", plid);
        map.put("member", member); 
         List<DoingPictorial> dpList =pictorialTopicBiz.getOneForcreator(map);
        if(dpList.size()>0){  
            return dpList.get(0);
        }else{
            return null;
       }
    }
}
